package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
* 游记服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository repsitory;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        repsitory.save(travel);
    }

    @Override
    public void update(Travel travel) {
        repsitory.save(travel);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        Travel travel = repsitory.findById(id).orElse(null);
       if(travel != null){
           Destination destination = destinationService.get(travel.getDestId());
           travel.setDest(destination);
           UserInfo userInfo = userInfoService.get(travel.getUserId());
           travel.setAuthor(userInfo);

       }
        return travel;
    }

    @Override
    public List<Travel> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //map(qo,(1,6))
        // 旅游天数
        TravelCondition condition = TravelCondition.DAYMAP.get(qo.getDayType());
        if(qo.getDayType() != -1){
            query.addCriteria(Criteria.where("day").gte(condition.getMin()).lte(condition.getMax()));
        }

        // 人均消费
        TravelCondition travelCondition = TravelCondition.PEREXPENDTYPEMAP.get(qo.getPerExpendType());
        if(qo.getPerExpendType() != -1){
            query.addCriteria(Criteria.where("day").gte(travelCondition.getMin()).lte(travelCondition.getMax()));
        }

        // 最热最新
        Pageable pageable = null;
        if(qo.getOrderType() == 1){
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        }else if(qo.getOrderType() == 2){
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "viewnum");
        }else {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "_id");
        }

        Page<Travel> pages = DBHelper.query(template, pageable, query, Travel.class, qo);
        for (Travel page : pages) {
            UserInfo userInfo = userInfoService.get(page.getUserId());
            page.setAuthor(userInfo);
        }
        return pages;
    }

    @Override
    public void audit(String id, int state) {
        Travel travel = this.get(id);
        //- 判断是否满足审核条件
        // 对象不为空，状态不是审核中
        if(travel == null || travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("参数异常");
        }
        //  - 状态为审核通过
        if(state == Travel.STATE_RELEASE){
            //游记状态改为发布
            travel.setState(Travel.STATE_RELEASE);
            //    - 游记发布时间为当前时间
            travel.setReleaseTime(new Date());
            //    - 游记的最后修改时间
            travel.setLastUpdateTime(new Date());
            // - 记录审核的相关信息  1. 审核人2. 审核时间3. 审核意见.............

            //- 审核拒绝后要做什么
        }else if(state == Travel.STATE_REJECT){
            //  ​	状态为拒绝审核
            travel.setState(Travel.STATE_REJECT);
            //    - 游记发布时间为当前时间
            travel.setReleaseTime(null);
            //    - 游记的最后修改时间
            travel.setLastUpdateTime(new Date());
            // - 记录审核的相关信息  1. 审核人2. 审核时间3. 审核意见.............
        }else {
            throw new LogicException("参数异常");
        }
        this.update(travel);

    }

    @Override
    public List<Travel> queryByDestName(String destName) {
        List<Travel> travels = repsitory.findByDestName(destName);
        for (Travel travel : travels) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travels;
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        //目的地名称
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());
        // 用户id
        UserInfo author = travel.getAuthor();
        travel.setUserId(author.getId());
        //创建时间
        travel.setCreateTime(new Date());
        // 发布时间
        travel.setReleaseTime(null);
        // 最后修改时间
        travel.setLastUpdateTime(new Date());
        if(StringUtils.hasLength(travel.getId())){
            //update
            // 查询
            Travel tr = repsitory.findById(travel.getId()).orElse(null);
            //替换
            travel.setIsPublic(tr.getIsPublic());
            travel.setViewnum(tr.getViewnum());
            travel.setReplynum(tr.getReplynum());
            travel.setFavornum(tr.getFavornum());
            travel.setFavornum(tr.getFavornum());
            travel.setThumbsupnum(tr.getThumbsupnum());
            this.update(travel);
        }else{
            //save
            this.save(travel);
        }
    }


}
